package com.cy.service;

import com.cy.pojo.RedisPojo;
import com.cy.util.RedisGeneratorUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/15.
 */
@Service
public class MyRedisService extends RedisGeneratorUtil<String, String>{

    /**
     * 增加缓存
     * @param redisPojo
     * @return
     */
    public synchronized boolean add(RedisPojo redisPojo) {
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key  = serializer.serialize(redisPojo.getId());
                byte[] name = serializer.serialize(redisPojo.getNickname());
                connection.set(key, name);
                connection.close();
                return true;
            }
        });
        return result;
    }

    /**
     * 添加集合
     */
    public synchronized boolean add(List<RedisPojo> list) {
        Assert.notEmpty(list);
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                for (RedisPojo redisPojo : list) {
                    byte[] key  = serializer.serialize(redisPojo.getId());
                    byte[] name = serializer.serialize(redisPojo.getNickname());
                    connection.set(key, name);
                }
                connection.close();
                return true;
            }
        }, false, true);
        return result;
    }

    /**
     * 删除对象 ,依赖key
     */
    public synchronized void delete(String key) {
        List<String> list = new ArrayList<String>();
        list.add(key);
        delete(list);
    }

    /**
     * 删除集合 ,依赖key集合
     */
    public synchronized void delete(List<String> keys) {
        redisTemplate.delete(keys);
    }

    /**
     * 修改对象 
     */
    public synchronized boolean update(RedisPojo redisPojo) {
        String key = redisPojo.getId();
        if (get(key) == null) {
            throw new NullPointerException("数据行不存在, key = " + key);
        }
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key  = serializer.serialize(redisPojo.getId());
                byte[] name = serializer.serialize(redisPojo.getNickname());
                connection.set(key, name);
                connection.close();
                return true;
            }
        });
        return result;
    }

    /**
     * 根据key获取对象
     */
    public synchronized RedisPojo get(String keyId) {
        RedisPojo result = redisTemplate.execute(new RedisCallback<RedisPojo>() {
            public RedisPojo doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(keyId);
                byte[] value = connection.get(key);
                if (value == null) {
                    return null;
                }
                String nickname = serializer.deserialize(value);
                connection.close();
                return new RedisPojo(keyId, nickname);
            }
        });
        return result;
    }
}